/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package cn.knet.sp.ncrm.modules.office.notify.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import cn.knet.sp.ncrm.common.config.Global;
import cn.knet.sp.ncrm.common.persistence.Page;
import cn.knet.sp.ncrm.common.web.BaseController;
import cn.knet.sp.ncrm.common.utils.Encodes;
import cn.knet.sp.ncrm.common.utils.StringUtils;
import cn.knet.sp.ncrm.modules.office.notify.entity.OfficeNotifyRecord;
import cn.knet.sp.ncrm.modules.office.notify.service.OfficeNotifyRecordService;
import cn.knet.sp.ncrm.modules.sys.utils.UserUtils;

/**
 * 系统通知、预约提醒记录Controller
 * @author gehaiyang
 * @version 2015-07-20
 */
@Controller
@RequestMapping(value = "${adminPath}/office/notify/officeNotifyRecord")
public class OfficeNotifyRecordController extends BaseController {

	@Autowired
	private OfficeNotifyRecordService officeNotifyRecordService;
	
	@ModelAttribute
	public OfficeNotifyRecord get(@RequestParam(required=false) String id) {
		OfficeNotifyRecord entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = officeNotifyRecordService.get(id);
		}
		if (entity == null){
			entity = new OfficeNotifyRecord();
		}
		return entity;
	}
	
	@RequestMapping(value = {"list", ""})
	public String list(OfficeNotifyRecord officeNotifyRecord, HttpServletRequest request, HttpServletResponse response, Model model) {
		officeNotifyRecord.setUser(UserUtils.getUser());
		Page<OfficeNotifyRecord> page = officeNotifyRecordService.findPage(new Page<OfficeNotifyRecord>(request, response), officeNotifyRecord); 
		model.addAttribute("page", page);
		return "modules/office/notify/officeNotifyRecordList";
	}

	@RequestMapping(value = "view")
	public String view(OfficeNotifyRecord officeNotifyRecord, Model model) {
		String content = officeNotifyRecord.getOfficeNotify().getContent();
		officeNotifyRecord.getOfficeNotify().setContent(Encodes.unescapeHtml(content));
		model.addAttribute("officeNotifyRecord", officeNotifyRecord);
		if ("0".equals(officeNotifyRecord.getReadFlag())) {
				officeNotifyRecordService.read(officeNotifyRecord);//设置已读状态
		}
		return "modules/office/notify/officeNotifyRecordView";
	}

	@RequiresPermissions("office:officeNotifyRecord:edit")
	@RequestMapping(value = "save")
	public String save(OfficeNotifyRecord officeNotifyRecord, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, officeNotifyRecord)){
			return view(officeNotifyRecord, model);
		}
		officeNotifyRecordService.save(officeNotifyRecord);
		addMessage(redirectAttributes, "保存通知成功");
		return "redirect:"+Global.getAdminPath()+"/office/notify/officeNotifyRecord/?repage";
	}
	
	@RequestMapping(value = "read")
	public String read(OfficeNotifyRecord officeNotifyRecord, RedirectAttributes redirectAttributes) {
		officeNotifyRecordService.read(officeNotifyRecord);
		addMessage(redirectAttributes, "标记已读成功");
		return "redirect:"+Global.getAdminPath()+"/office/notify/officeNotifyRecord/?repage";
	}
}